package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.util.StringUtil;


/**
 * 访问
 * @author tngou@tngou.net
 *
 */
public class AccessDao extends BaseDao {

	
	/**
	 * 取得访问列表
	 * @param page
	 * @param size
	 * @param main
	 * @param type
	 * @param keyword
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,int type,String keyword) {

		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT patient.no,patient.name,patient.gender,patient.birthday,access.*,survey.name as pname,survey.description ";
		String sql_t="SELECT COUNT(*) ";
		String sql_o=" ORDER BY access.id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_access` access ,`"+POJO.DABASE+"_patient` patient ,`"+POJO.DABASE+"_survey` survey";
		sql=sql+ " WHERE  access.main=? AND access.type=?  AND patient.id=access.patient AND survey.id=access.survey ";
		if(StringUtil.isNotEmpty(keyword))
		{
			sql=sql+" AND (patient.no LIKE ? OR patient.name  LIKE ? OR survey.name  LIKE ?)";
			
			Object[] params={main,type,keyword+"%","%"+keyword+"%","%"+keyword+"%"};
			list=QueryHelper.query_page(sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else{
			Object[] params={main,type};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);	
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}		
		return new PageUtil(list, page, size, total);
		
	}
	
	public PageUtil getPage(int page,int size,long main,long disease) {

		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT patient.no,patient.name,patient.gender,patient.birthday,access.*,survey.name as pname,survey.description ";
		String sql_t="SELECT COUNT(*) ";
		String sql_o=" ORDER BY access.id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_access` access ,`"+POJO.DABASE+"_cure` cure ,`"+POJO.DABASE+"_patient` patient ,`"+POJO.DABASE+"_survey` survey";
		sql=sql+ " WHERE  access.main=? AND cure.disease=?   AND access.cure=cure.id  AND patient.id=access.patient AND survey.id=access.survey ";
		
		Object[] params={main,disease};
		list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);	
		total=(int) QueryHelper.stat(sql_t+sql, params);
			
		return new PageUtil(list, page, size, total);
		
	}
	public PageUtil getPage(int page,int size,long main,int type,int state,String keyword) {

		List<Map<String, Object>> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT patient.no,patient.name,patient.gender,patient.birthday,access.*,survey.name as pname,survey.description ";
		String sql_t="SELECT COUNT(*) ";
		String sql_o=" ORDER BY access.id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_access` access ,`"+POJO.DABASE+"_patient` patient ,`"+POJO.DABASE+"_survey` survey";
		sql=sql+ " WHERE  access.main=? AND access.type=? AND access.state=? AND patient.id=access.patient AND survey.id=access.survey ";
		if(StringUtil.isNotEmpty(keyword))
		{
			sql=sql+" AND (patient.no LIKE ? OR patient.name  LIKE ? OR survey.name  LIKE ?)";
			
			Object[] params={main,type,state,keyword+"%","%"+keyword+"%","%"+keyword+"%"};
			list=QueryHelper.query_page(sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else{
			Object[] params={main,type,state};
			list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);	
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}		
		return new PageUtil(list, page, size, total);
		
	}
	
	/**
	 * 取得患者访问
	 * @param main
	 * @param patient
	 * @param type
	 * @return
	 */
	public List<Map<String, Object>> getList(long main,long patient,int type) {
		String sql="SELECT access.*,survey.name FROM `"+POJO.DABASE+"_access` access ,`"+POJO.DABASE+"_survey` survey WHERE access.survey=survey.id AND access.main=? AND access.patient=? AND access.type=?";
		return QueryHelper.query(sql, main,patient,type);
	}
	/**
	 * 取得治疗随访
	 * @param cure
	 * @return
	 */
	public PageUtil getList(int page,int size,long cure) {
		List<Map<String, Object>> list =new ArrayList<>();
		int total=0;
		String sql_r="SELECT access.*,survey.name ";
		String sql_t="SELECT COUNT(*) ";
		String sql_o=" ORDER BY access.id DESC";//排序
		
		String sql=" FROM `"+POJO.DABASE+"_access` access ,`"+POJO.DABASE+"_survey` survey";
		sql=sql+ " WHERE access.survey=survey.id AND access.cure=? ";
		Object[] params={cure};
		list=QueryHelper.query_page( sql_r+sql+sql_o, page, size, params);	
		total=(int) QueryHelper.stat(sql_t+sql, params);
		return new PageUtil(list, page, size, total);
	}

	/**
	 * 取得信息
	 * @param patient
	 * @param as
	 * @return
	 */
	public List<Accessresult> getAccessresult(long patient,Object ...as)
	{
		String in="";
		for (int i = 0; i < as.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		String sql="SELECT * FROM `iask_accessresult` WHERE patient=? AND  access IN("+in+")";
		as=ArrayUtils.add(as, 0, patient);
		return QueryHelper.query(Accessresult.class, sql, as);
	}
	
	
	/**
	 * 随访数据
	 * @param main
	 * @param question
	 * @param ps
	 * @return
	 */
	public int access(long main,long question,Object ...ps) {
		String in="";
		for (int i = 0; i < ps.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		ps=ArrayUtils.add(ps, 0, main);
		ps=ArrayUtils.add(ps, question);
		String sql="SELECT COUNT(DISTINCT access.id) FROM `iask_access` access,`iask_accessresult` accessresult WHERE access.id=accessresult.access AND access.main=? AND access.patient IN("+in+")  AND accessresult.question=? ";
		return (int) QueryHelper.stat(sql, ps);
	}
	
	/**
	 * 
	 * @param main
	 * @param question
	 * @param ps
	 * @return
	 */
	public int accessMax(long main,long question,Object ...ps) {
		String in="";
		for (int i = 0; i < ps.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		ps=ArrayUtils.add(ps, 0, main);
		ps=ArrayUtils.add(ps,  question);
		String sql="SELECT MAX(DISTINCT access.id) FROM `iask_access` access,`iask_accessresult` accessresult WHERE access.id=accessresult.access AND access.main=? AND access.patient IN("+in+")  AND accessresult.question=? GROUP BY access.patient";
		return (int) QueryHelper.stat(sql,ps);
	}
	
	/**
	 * 
	 * @param main
	 * @param question
	 * @param ps
	 * @return
	 */
	public int accessMin(long main,long question,Object ...ps) {
		String in="";
		for (int i = 0; i < ps.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		String sql="SELECT MAX(DISTINCT access.id) FROM `iask_access` access,`iask_accessresult` accessresult WHERE access.id=accessresult.access AND access.main=? AND access.patient IN("+in+")  AND accessresult.question=? GROUP BY access.patient";
		ps=ArrayUtils.add(ps, 0, main);
		ps=ArrayUtils.add(ps,  question);
		return (int) QueryHelper.stat(sql,ps);
	}
	
	/**
	 * 
	 * @param main
	 * @param question
	 * @param ps
	 * @return
	 */
	public int accessresult(long main,long question,Object ...ps) {
		String in="";
		for (int i = 0; i < ps.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		ps=ArrayUtils.add(ps, 0, main);
		ps=ArrayUtils.add(ps,  question);
		String sql="SELECT MAX(accessresult.id) FROM `iask_accessresult` accessresult,`iask_access` access WHERE access.id=accessresult.access AND access.main=? AND access.patient IN("+in+")  AND accessresult.question=?";
		return (int) QueryHelper.stat(sql,ps);
	}
	
	/**
	 * 结果
	 * @param main
	 * @param question
	 * @param ps
	 * @return
	 */
	public int accessresultvalue(long main,long question,Object ...ps) {
		String in="";
		for (int i = 0; i < ps.length; i++) {
			if(StringUtil.isNotEmpty(in))in=in+",";
			in=in+"?";
		}
		ps=ArrayUtils.add(ps, 0, main);
		ps=ArrayUtils.add(ps,  question);
		String sql="SELECT MAX(DISTINCT value) FROM `iask_accessresult` accessresult,`iask_access` access WHERE access.id=accessresult.access AND access.main=? AND access.patient IN("+in+")  AND accessresult.question=?";
		return (int) QueryHelper.stat(sql,ps);
	}
}
